﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeGenerator.CSharp
{
    /// <summary>
    /// Identifies a single documentation item.
    /// </summary>
    public class DocumentationItem
    {
        public string Name { get; set; }
        public string Text { get; set; }

        public string ToString(string padding)
        {
            CodeBuilder Code = new CodeBuilder();
            Code.Add(padding + "<" + this.Name + ">");
            Code.Add(Utilities.ParseText(this.Text, padding));
            Code.Append(padding + "</" + this.Name + ">");
            return Code.ToString();
        }

    }

    /// <summary>
    /// Identifies a collection of DocumentationItems, suitable to document a code unit.
    /// </summary>
    public class Documentation : List<DocumentationItem>
    {

        #region Add

        /// <summary>
        /// Creates, adds and returns a new DocumentationItem using the identified name and text. 
        /// </summary>
        public DocumentationItem Add(string name, string text)
        {
            DocumentationItem ThisItem = new DocumentationItem() { Name = name, Text = text };
            this.Add(ThisItem);
            return ThisItem;
        }

        #endregion

        #region Summary

        public string Summary
        {
            get
            {
                if (this._Summary != null)
                    return this._Summary.Text;
                else
                    return string.Empty;
            }
            set
            {
                if (this._Summary == null)
                    this._Summary = this.Add(("summary"), value);
                else
                    this._Summary.Text = value;
            }
        }
        private DocumentationItem _Summary;
        
        #endregion

        #region Remarks

        public string Remarks 
        {
            get 
            {
                if (this._Remarks != null)
                    return this._Remarks.Text;
                else
                    return string.Empty;
            }
            set 
            {
                if (this._Remarks == null)
                    this._Remarks = this.Add(("remarks"), value);
                else
                    this._Remarks.Text = value;
            }
        }
        private DocumentationItem _Remarks;

        #endregion

        #region Returns

        public string Returns
        {
            get
            {
                if (this._Returns != null)
                    return this._Returns.Text;
                else
                    return string.Empty;
            }
            set
            {
                if (this._Returns == null)
                    this._Returns = this.Add(("returns"), value);
                else
                    this._Returns.Text = value;
            }
        }
        private DocumentationItem _Returns;

        #endregion

        #region ToString

        public override string ToString() { return this.ToString(0); }

        public string ToString(int indentationLevel)
        {
            try
            {
                string DocumentationMarker = "///";
                string Padding = new string(char.Parse(" "), indentationLevel * Settings.CharactersPerTab) + DocumentationMarker + " ";

                CodeBuilder Code = new CodeBuilder();
                foreach (DocumentationItem ThisItem in this)
                    Code.Add(ThisItem.ToString(Padding));

                return Code.ToString();
            }
            catch (Exception)
            {
                return string.Empty;
                //throw ex;
            }

        }

        #endregion


    }

}
